@echo off
chcp 65001 >nul

echo 🔧 修复 MySQL JSON 兼容性问题...

REM 备份原文件
copy prisma\schema.prisma prisma\schema.prisma.backup

REM 创建修复后的 schema.prisma 文件
(
echo generator client {
echo   provider = "prisma-client-js"
echo }
echo.
echo datasource db {
echo   provider     = "mysql"
echo   url          = env("DATABASE_URL"^)
echo   relationMode = "prisma"
echo }
echo.
echo model User {
echo   id            Int            @id @default(autoincrement(^)^)
echo   username      String         @unique @db.VarChar(50^)
echo   email         String         @unique @db.VarChar(100^)
echo   password      String         @db.VarChar(255^)
echo   realName      String?        @map("real_name"^) @db.VarChar(100^)
echo   avatar        String?        @db.VarChar(255^)
echo   phone         String?        @db.VarChar(20^)
echo   status        UserStatus     @default(ACTIVE^)
echo   lastLoginAt   DateTime?      @map("last_login_at"^)
echo   createdAt     DateTime       @default(now(^)^) @map("created_at"^)
echo   updatedAt     DateTime       @updatedAt @map("updated_at"^)
echo   userRoles     UserRole[]
echo   operationLogs OperationLog[]
echo   
echo   // 音乐节助手相关关联
echo   votes         Vote[]
echo   userBadges    UserBadge[]
echo   userLocations UserLocation[]
echo   notifications Notification[]
echo   notificationSettings UserNotificationSettings?
echo   assistantConversations AssistantConversation[]
echo.
echo   @@map("users"^)
echo }
echo.
echo model Role {
echo   id              Int              @id @default(autoincrement(^)^)
echo   name            String           @unique @db.VarChar(50^)
echo   description     String?          @db.Text
echo   permissions     String?          @db.Text // 改为 Text 类型以兼容旧版 MySQL
echo   status          RoleStatus       @default(ACTIVE^)
echo   createdAt       DateTime         @default(now(^)^) @map("created_at"^)
echo   updatedAt       DateTime         @updatedAt @map("updated_at"^)
echo   userRoles       UserRole[]
echo   rolePermissions RolePermission[]
echo.
echo   @@map("roles"^)
echo }
echo.
echo model UserRole {
echo   id        Int      @id @default(autoincrement(^)^)
echo   userId    Int      @map("user_id"^)
echo   roleId    Int      @map("role_id"^)
echo   createdAt DateTime @default(now(^)^) @map("created_at"^)
echo   user      User     @relation(fields: [userId], references: [id], onDelete: Cascade^)
echo   role      Role     @relation(fields: [roleId], references: [id], onDelete: Cascade^)
echo.
echo   @@unique([userId, roleId]^)
echo   @@map("user_roles"^)
echo }
echo.
echo model Permission {
echo   id              Int              @id @default(autoincrement(^)^)
echo   name            String           @unique @db.VarChar(100^)
echo   code            String           @unique @db.VarChar(100^)
echo   description     String?          @db.Text
echo   module          String           @db.VarChar(50^)
echo   createdAt       DateTime         @default(now(^)^) @map("created_at"^)
echo   updatedAt       DateTime         @updatedAt @map("updated_at"^)
echo   rolePermissions RolePermission[]
echo.
echo   @@map("permissions"^)
echo }
echo.
echo model RolePermission {
echo   id           Int        @id @default(autoincrement(^)^)
echo   roleId       Int        @map("role_id"^)
echo   permissionId Int        @map("permission_id"^)
echo   createdAt    DateTime   @default(now(^)^) @map("created_at"^)
echo   role         Role       @relation(fields: [roleId], references: [id], onDelete: Cascade^)
echo   permission   Permission @relation(fields: [permissionId], references: [id], onDelete: Cascade^)
echo.
echo   @@unique([roleId, permissionId]^)
echo   @@map("role_permissions"^)
echo }
echo.
echo model SystemConfig {
echo   id          Int        @id @default(autoincrement(^)^)
echo   key         String     @unique @db.VarChar(100^)
echo   value       String     @db.Text
echo   description String?    @db.Text
echo   type        ConfigType @default(STRING^)
echo   createdAt   DateTime   @default(now(^)^) @map("created_at"^)
echo   updatedAt   DateTime   @updatedAt @map("updated_at"^)
echo.
echo   @@map("system_configs"^)
echo }
echo.
echo model OperationLog {
echo   id         Int      @id @default(autoincrement(^)^)
echo   userId     Int?     @map("user_id"^)
echo   action     String   @db.VarChar(100^)
echo   resource   String   @db.VarChar(100^)
echo   resourceId String?  @map("resource_id"^) @db.VarChar(100^)
echo   details    String?  @db.Text // 改为 Text 类型以兼容旧版 MySQL
echo   ipAddress  String?  @map("ip_address"^) @db.VarChar(45^)
echo   userAgent  String?  @map("user_agent"^) @db.Text
echo   createdAt  DateTime @default(now(^)^) @map("created_at"^)
echo   user       User?    @relation(fields: [userId], references: [id], onDelete: SetNull^)
echo.
echo   @@map("operation_logs"^)
echo }
echo.
echo // 音乐节助手应用相关模型
echo.
echo model Artist {
echo   id          Int           @id @default(autoincrement(^)^)
echo   name        String        @db.VarChar(100^)
echo   description String?       @db.Text
echo   avatar      String?       @db.VarChar(255^)
echo   bannerImage String?       @map("banner_image"^) @db.VarChar(255^)
echo   genre       String?       @db.VarChar(50^)
echo   country     String?       @db.VarChar(50^)
echo   status      ArtistStatus  @default(ACTIVE^)
echo   createdAt   DateTime      @default(now(^)^) @map("created_at"^)
echo   updatedAt   DateTime      @updatedAt @map("updated_at"^)
echo   
echo   // 关联
echo   performances Performance[]
echo   votes       Vote[]
echo   works       ArtistWork[]
echo   trivia      Trivia[]
echo.
echo   @@map("artists"^)
echo }
echo.
echo model Performance {
echo   id              Int              @id @default(autoincrement(^)^)
echo   artistId        Int              @map("artist_id"^)
echo   title           String           @db.VarChar(200^)
echo   description     String?          @db.Text
echo   venue           String           @db.VarChar(100^)
echo   floor           String?          @db.VarChar(20^)
echo   startTime       DateTime         @map("start_time"^)
echo   endTime         DateTime         @map("end_time"^)
echo   status          PerformanceStatus @default(SCHEDULED^)
echo   maxCapacity     Int?             @map("max_capacity"^)
echo   currentAttendance Int?           @map("current_attendance"^) @default(0^)
echo   createdAt       DateTime         @default(now(^)^) @map("created_at"^)
echo   updatedAt       DateTime         @updatedAt @map("updated_at"^)
echo   
echo   // 关联
echo   artist          Artist           @relation(fields: [artistId], references: [id], onDelete: Cascade^)
echo.
echo   @@map("performances"^)
echo }
echo.
echo model Vote {
echo   id        Int      @id @default(autoincrement(^)^)
echo   userId    Int      @map("user_id"^)
echo   artistId  Int      @map("artist_id"^)
echo   voteCount Int      @default(1^) @map("vote_count"^)
echo   createdAt DateTime @default(now(^)^) @map("created_at"^)
echo   
echo   // 关联
echo   user      User     @relation(fields: [userId], references: [id], onDelete: Cascade^)
echo   artist    Artist   @relation(fields: [artistId], references: [id], onDelete: Cascade^)
echo.
echo   @@unique([userId, artistId]^)
echo   @@map("votes"^)
echo }
echo.
echo model Badge {
echo   id             Int      @id @default(autoincrement(^)^)
echo   name           String   @db.VarChar(100^)
echo   description    String?  @db.Text
echo   icon           String?  @db.VarChar(100^)
echo   type           String?  @db.VarChar(50^)
echo   conditionType  String?  @map("condition_type"^) @db.VarChar(50^)
echo   conditionValue Int?     @map("condition_value"^)
echo   createdAt      DateTime @default(now(^)^) @map("created_at"^)
echo   
echo   // 关联
echo   userBadges     UserBadge[]
echo.
echo   @@map("badges"^)
echo }
echo.
echo model UserBadge {
echo   id        Int      @id @default(autoincrement(^)^)
echo   userId    Int      @map("user_id"^)
echo   badgeId   Int      @map("badge_id"^)
echo   earnedAt  DateTime @default(now(^)^) @map("earned_at"^)
echo   
echo   // 关联
echo   user      User     @relation(fields: [userId], references: [id], onDelete: Cascade^)
echo   badge     Badge    @relation(fields: [badgeId], references: [id], onDelete: Cascade^)
echo.
echo   @@unique([userId, badgeId]^)
echo   @@map("user_badges"^)
echo }
echo.
echo model Facility {
echo   id          Int            @id @default(autoincrement(^)^)
echo   name        String         @db.VarChar(100^)
echo   type        String         @db.VarChar(50^)
echo   floor       String?        @db.VarChar(20^)
echo   locationX   Decimal?       @map("location_x"^) @db.Decimal(10, 6^)
echo   locationY   Decimal?       @map("location_y"^) @db.Decimal(10, 6^)
echo   description String?        @db.Text
echo   status      FacilityStatus @default(OPEN^)
echo   createdAt   DateTime       @default(now(^)^) @map("created_at"^)
echo   updatedAt   DateTime       @updatedAt @map("updated_at"^)
echo.
echo   @@map("facilities"^)
echo }
echo.
echo model Trivia {
echo   id        Int      @id @default(autoincrement(^)^)
echo   title     String   @db.VarChar(200^)
echo   content   String   @db.Text
echo   category  String?  @db.VarChar(50^)
echo   artistId  Int?     @map("artist_id"^)
echo   isActive  Boolean  @default(true^) @map("is_active"^)
echo   createdAt DateTime @default(now(^)^) @map("created_at"^)
echo   
echo   // 关联
echo   artist    Artist?  @relation(fields: [artistId], references: [id], onDelete: SetNull^)
echo.
echo   @@map("trivia"^)
echo }
echo.
echo model AssistantConversation {
echo   id        Int      @id @default(autoincrement(^)^)
echo   userId    Int      @map("user_id"^)
echo   sessionId String   @unique @map("session_id"^) @db.VarChar(100^)
echo   createdAt DateTime @default(now(^)^) @map("created_at"^)
echo   updatedAt DateTime @updatedAt @map("updated_at"^)
echo   
echo   // 关联
echo   user      User     @relation(fields: [userId], references: [id], onDelete: Cascade^)
echo   messages  AssistantMessage[]
echo.
echo   @@map("assistant_conversations"^)
echo }
echo.
echo model AssistantMessage {
echo   id             Int                @id @default(autoincrement(^)^)
echo   conversationId Int                @map("conversation_id"^)
echo   messageType    AssistantMessageType @map("message_type"^)
echo   content        String             @db.Text
echo   messageFormat  AssistantMessageFormat @default(TEXT^) @map("message_format"^)
echo   createdAt      DateTime           @default(now(^)^) @map("created_at"^)
echo   
echo   // 关联
echo   conversation   AssistantConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade^)
echo.
echo   @@map("assistant_messages"^)
echo }
echo.
echo model UserLocation {
echo   id        Int      @id @default(autoincrement(^)^)
echo   userId    Int      @map("user_id"^)
echo   latitude  Decimal  @db.Decimal(10, 8^)
echo   longitude Decimal  @db.Decimal(11, 8^)
echo   floor     String?  @db.VarChar(20^)
echo   accuracy  Decimal? @db.Decimal(5, 2^)
echo   createdAt DateTime @default(now(^)^) @map("created_at"^)
echo   
echo   // 关联
echo   user      User     @relation(fields: [userId], references: [id], onDelete: Cascade^)
echo.
echo   @@map("user_locations"^)
echo }
echo.
echo model Notification {
echo   id         Int              @id @default(autoincrement(^)^)
echo   userId     Int              @map("user_id"^)
echo   title      String           @db.VarChar(200^)
echo   content    String           @db.Text
echo   type       NotificationType
echo   isRead     Boolean          @default(false^) @map("is_read"^)
echo   relatedId  Int?             @map("related_id"^)
echo   relatedType String?         @map("related_type"^) @db.VarChar(50^)
echo   createdAt  DateTime         @default(now(^)^) @map("created_at"^)
echo   
echo   // 关联
echo   user       User             @relation(fields: [userId], references: [id], onDelete: Cascade^)
echo.
echo   @@map("notifications"^)
echo }
echo.
echo model UserNotificationSettings {
echo   id                  Int      @id @default(autoincrement(^)^)
echo   userId              Int      @unique @map("user_id"^)
echo   pushEnabled         Boolean  @default(true^) @map("push_enabled"^)
echo   emailEnabled        Boolean  @default(true^) @map("email_enabled"^)
echo   performanceReminder Boolean  @default(true^) @map("performance_reminder"^)
echo   voteReminder        Boolean  @default(true^) @map("vote_reminder"^)
echo   createdAt           DateTime @default(now(^)^) @map("created_at"^)
echo   updatedAt           DateTime @updatedAt @map("updated_at"^)
echo   
echo   // 关联
echo   user                User     @relation(fields: [userId], references: [id], onDelete: Cascade^)
echo.
echo   @@map("user_notification_settings"^)
echo }
echo.
echo model ArtistWork {
echo   id              Int      @id @default(autoincrement(^)^)
echo   artistId        Int      @map("artist_id"^)
echo   title           String   @db.VarChar(200^)
echo   duration        String?  @db.VarChar(10^)
echo   description     String?  @db.Text
echo   releaseDate     DateTime? @map("release_date"^)
echo   isRepresentative Boolean @default(false^) @map("is_representative"^)
echo   createdAt       DateTime @default(now(^)^) @map("created_at"^)
echo   
echo   // 关联
echo   artist          Artist   @relation(fields: [artistId], references: [id], onDelete: Cascade^)
echo.
echo   @@map("artist_works"^)
echo }
echo.
echo enum UserStatus {
echo   ACTIVE
echo   INACTIVE
echo   BANNED
echo }
echo.
echo enum RoleStatus {
echo   ACTIVE
echo   INACTIVE
echo }
echo.
echo enum ConfigType {
echo   STRING
echo   NUMBER
echo   BOOLEAN
echo   TEXT
echo }
echo.
echo enum ArtistStatus {
echo   ACTIVE
echo   INACTIVE
echo }
echo.
echo enum PerformanceStatus {
echo   SCHEDULED
echo   ONGOING
echo   COMPLETED
echo   CANCELLED
echo }
echo.
echo enum FacilityStatus {
echo   OPEN
echo   CLOSED
echo   MAINTENANCE
echo }
echo.
echo enum AssistantMessageType {
echo   USER
echo   ASSISTANT
echo }
echo.
echo enum AssistantMessageFormat {
echo   TEXT
echo   VOICE
echo   IMAGE
echo }
echo.
echo enum NotificationType {
echo   PERFORMANCE
echo   VOTE
echo   SYSTEM
echo   PROMOTION
echo }
) > prisma\schema.prisma

echo ✅ Schema 文件已修复
echo 📁 原文件已备份为 prisma\schema.prisma.backup
echo 💡 主要修改：
echo    - 将 Json? 类型改为 String? @db.Text
echo    - 将 ConfigType.JSON 改为 ConfigType.TEXT
echo    - 保持数据兼容性

REM 重新生成 Prisma 客户端
echo 🔧 重新生成 Prisma 客户端...
call npx prisma generate

echo ✅ 修复完成！
echo 💡 现在可以运行数据库迁移了

pause
